/**
 * 画笔绘图工具 by zuozhengqi 2017/02/09
 */
import d3 from 'd3'

export function hasOwnProperty (obj, prop) {
  return Array.prototype.hasOwnProperty.call(obj, prop)
}

// => 取消所有画线的鼠标事件
export function cancelPointerEvents () {
  // 线段
  d3.selectAll('.lineSegment').attr({
    'pointer-events': 'none'
  })
  d3.selectAll('.lineSegmentStart').attr({
    'pointer-events': 'none'
  })
  d3.selectAll('.lineSegmentEnd').attr({
    'pointer-events': 'none'
  })
}

// => 恢复所有画线的鼠标事件
export function recoverPointerEvents () {
  // 线段
  d3.selectAll('.lineSegment').attr({
    'pointer-events': 'auto'
  })
  d3.selectAll('.lineSegmentStart').attr({
    'pointer-events': 'auto'
  })
  d3.selectAll('.lineSegmentEnd').attr({
    'pointer-events': 'auto'
  })
}

// => 重新计算所有画线数据
export function reCaculUserChart () {
  console.log('reCaculUserChart')
}

// => 绘制所有画线
export function chartToolMain (uStore, payload) {
  console.log('chartToolMain', uStore)
  console.log('payload', payload)
  payload.lists.forEach((d) => {
    let G = d3.select(`.${d}PanelG`)
    if (hasOwnProperty(uStore.lineSegments, d) && uStore.lineSegments[d].length > 0) {
      drawLineSegments(G, uStore.lineSegments[d], payload.c1Drag, payload.c2Drag)
    }
  })
}

// => 绘制线段
export function drawLineSegments (G, data, c1Drag, c2Drag) {
  let updateLines = G.selectAll(`.lineSegment`)
    .data(data)
  let enterLines = updateLines.enter()
  let exitLines = updateLines.exit()

  updateLines.attr({
    x1: d => {
      return d.co[0].x
    },
    y1: d => {
      return d.co[0].y
    },
    x2: d => {
      return d.co[1].x
    },
    y2: d => {
      return d.co[1].y
    },
    fill: `none`,
    stroke: d => {
      return d.stroke
    },
    'stroke-width': d => {
      return d.size
    },
  })
  enterLines.append('line')
    .attr({
      class: `lineSegment`,
      'data-index': d => {
        return d.id
      },
      x1: d => {
        return d.co[0].x
      },
      y1: d => {
        return d.co[0].y
      },
      x2: d => {
        return d.co[1].x
      },
      y2: d => {
        return d.co[1].y
      },
      cursor: `move`,
      fill: `none`,
      stroke: d => {
        return d.stroke
      },
      'stroke-width': d => {
        return d.size
      },
      'pointer-events': 'none'
    })
  exitLines.remove()

  let updateCircleStart = G.selectAll('.lineSegmentStart')
    .data(data)
  let enterCircleStart = updateCircleStart.enter()
  let exitCircleStart = updateCircleStart.exit()

  updateCircleStart.attr({
    cx: d => {
      return d.co[0].x
    },
    cy: d => {
      return d.co[0].y
    }
  })
  enterCircleStart.append('circle')
    .attr({
      class: `lineSegmentStart`,
      cx: d => {
        return d.co[0].x
      },
      cy: d => {
        return d.co[0].y
      },
      r: 2.5,
      fill: `#fff`,
      stroke: `#606060`,
      'stroke-width': 1,
      'pointer-events': 'none'
    })
    .call(c1Drag)
  exitCircleStart.remove()

  let updateCircleEnd = G.selectAll('.lineSegmentEnd')
    .data(data)
  let enterCircleEnd = updateCircleEnd.enter()
  let exitCircleEnd = updateCircleEnd.exit()

  updateCircleEnd.attr({
    cx: d => {
      return d.co[1].x
    },
    cy: d => {
      return d.co[1].y
    }
  })
  enterCircleEnd.append('circle')
    .attr({
      class: `lineSegmentEnd`,
      cx: d => {
        return d.co[1].x
      },
      cy: d => {
        return d.co[1].y
      },
      r: 2.5,
      fill: `#fff`,
      stroke: `#606060`,
      'stroke-width': 1,
      'pointer-events': 'none'
    })
    .call(c2Drag)
  exitCircleEnd.remove()
}

// => 计算一次函数k、b参数值
export function cacuKB (point1, point2) {
  if (point1.x !== point2.x && point1.y !== point2.y) {
    let k = (point1.y - point2.y) / (point1.x - point2.x)
    let b = point1.y - k * point1.x
    return [k, b]
  } else {
    return -1
  }
}

// => 计算一次函数坐标 val = {x: 100} || {y: 100}
export function cacuXY (val, args) {
  let k = args[0]
  let b = args[1]
  if (hasOwnProperty(val, 'x')) {
    return val.x * k + b
  } else if (hasOwnProperty(val, 'y')) {
    return (val.y - b) / k
  }
}

// => 判断值在区间中的位置
export function position (input, small, large) {
  return input >= small
    ? input <= large
      ? `center`
      : `right`
    : `left`
}

export function getNewCoor (p1, p2, p) {
  let len1 = Math.sqrt((Math.pow(p1[0] - p[0],2)) + (Math.pow(p1[1] - p[1],2)))
  let len2 = Math.sqrt((Math.pow(p2[0] - p[0],2)) + (Math.pow(p2[1] - p[1],2)))
  return len1 < len2 ? p1 : p2
}